﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wedo.Mvc.Utility;
using Wedo.Recognition.Domain.Models;
using System.IO; 
using Wedo.Recognition.Web.Controllers;
using Wedo.Recognition.Domain;
using Wedo.Recognition.Service;
using Wedo.Mvc.Pager;
using Wedo.Domain.Models;
using Wedo.Recognition.Domain;
using Wedo.Recognition.Repository;

namespace Wedo.IPRP.Web.Controllers
{
    public class RefundmentApplyController : EntityController<RefundmentApply, CommonSearchModel>
    {
        private BankDataService bdservice;

        private RefundmentApplyService service;
        public RefundmentApplyController(IUnitWork dbContext)
            : base(dbContext)
        {

            bdservice = new BankDataService(_dbContext);
            service = new RefundmentApplyService(_dbContext);
        }
         

        #region Finace 处理界面
        /// <summary>
        /// 带审批退款申请
        /// </summary>
        /// <returns></returns>
        public ActionResult WaitedRefundmentApply()
        {
            var where = new CommonSearchModel { Status = 1};
            ViewData["SearchModel"] = where;
            var list = GetSearchPagedList(where);
            return ReturnAjaxView("Index", list);
        }

        /// <summary>
        /// 所有退款申请
        /// </summary>
        /// <returns></returns>
        public override System.Web.Mvc.ActionResult Index()
        {
 
            return Search(new CommonSearchModel ());
        }

        /// <summary>
        /// 已同意退款申请
        /// </summary>
        /// <returns></returns>
        public ActionResult ApprovedRefundmentApply()
        {
            var where = new CommonSearchModel { Status = 2 };
            ViewData["SearchModel"] = where;
            var list = GetSearchPagedList(where);
            return ReturnAjaxView("List", list);
        }

        /// <summary>
        /// 已拒绝退款申请
        /// </summary>
        /// <returns></returns>
        public ActionResult RejectedRefundmentApply()
        {
            var where = new CommonSearchModel { Status =3 };
            ViewData["SearchModel"] = where;
            var list = GetSearchPagedList(where);
            return ReturnAjaxView("List", list);

        }

        #region 同意和拒绝操作
        [HttpPost]
        public ActionResult Agree(Guid id)
        {
            ResultMSG response = new ResultMSG();
            
            try
            {
                service.ApproveRefundment(CurrentUserName, id);
                response.Result =true;
                response.MSG = Wedo.Recognition.Resources.ErrorMessage.RefundApplicationsPass;
            }
            catch (Exception ex)
            {
                response.Result =false;
                response.MSG = ex.Message;
            }
            return Json(response);
        }

        [HttpPost]
        public ActionResult Reject(Guid id)
        {
            ResultMSG response = new ResultMSG(); 
            try
            {
                service.RejectedRefundment(CurrentUserName, id);
                response.Result =true;
                response.MSG = Wedo.Recognition.Resources.ErrorMessage.RefuseRefundApplication;
            }
            catch (Exception ex)
            {
                response.Result =false;
                response.MSG = ex.Message;
            }
            return Json(response);
        }
        #endregion
        #endregion

        #region CSR处理界面

        /// <summary>
        /// 当前CSR的退款申请
        /// </summary>
        /// <returns></returns>
        public ActionResult MyRefundmentApply()
        {
            var where = new CommonSearchModel {   ApplyMan = CurrentUserName };
            var list = GetSearchPagedList(where);
            
            ViewData["SearchModel"] = where;

            return ReturnAjaxView("Index", list);
        }

        /// <summary>
        /// 当前CSR等待审批的退款申请
        /// </summary>
        /// <returns></returns>
        public ActionResult MyWaitedRefundmentApply()
        {
            var where = new CommonSearchModel { Status = 1, ApplyMan = CurrentUserName };
            var list = GetSearchPagedList(where);

            ViewData["SearchModel"] = where;

            return ReturnAjaxView("Index", list);
        }

        /// <summary>
        /// 当前CSR已经审批通过的退款申请
        /// </summary>
        /// <returns></returns>
        public ActionResult MyApprovedRefundmentApply()
        {
            var where = new CommonSearchModel { Status = 2, ApplyMan = CurrentUserName };
            var list = GetSearchPagedList(where);

            ViewData["SearchModel"] = where;
            return ReturnAjaxView("Index", list);
        }

        /// <summary>
        /// 当前CSR被拒绝的退款申请
        /// </summary>
        /// <returns></returns>
        public ActionResult MyRejectedRefundmentApply()
        {
            var where = new CommonSearchModel { Status = 3, ApplyMan = CurrentUserName };
            var list = GetSearchPagedList(where);

            ViewData["SearchModel"] = where;
            return ReturnAjaxView("Index", list);
        }

        /// <summary>
        /// 撤消退款申请
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Cancel (Guid id)
        {
            ResultMSG response = new ResultMSG(); 
            try
            {
                service.CancelRefundment(CurrentUserName, id);
                response.Result =true;
                response.MSG = Wedo.Recognition.Resources.ErrorMessage.CancelRefundApplication;
            }
            catch (Exception ex)
            {
                response.Result =false;
                response.MSG = ex.Message;
            }
            return Json(response);
        }

        #endregion 
    
        protected override RefundmentApply FindById(Guid Id)
        {
            return service.GetAll(m => m.Id == Id,"BankData").FirstOrDefault();
        }

        protected override Wedo.Service.Common.IBaseService<RefundmentApply > EntityService
        {
            get { return service; }
        }

        protected override  PagedList<RefundmentApply> GetSearchPagedList(CommonSearchModel where)
        {
            where = where ?? new CommonSearchModel();
            //SetUserInfo();

            var query = service.GetAll(includes: "BankData");
            if (where.StartTime != null)
            {
                DateTime Start = where.StartTime.Value;
                query = query.Where(m => m.ApplyTime > Start);
            }
            if (where.EndTime != null)
            {
                DateTime endTime = where.EndTime.Value.AddDays(1);
                query = query.Where(m => m.ApplyTime < endTime);
            }

            if (where.Status != null)
                query = query.Where(m => m.Status == where.Status);

            if (where.ApplyMan.IsNotNullOrWhiteSpace())
            {
                query = query.Where(m => m.ApplyMan == where.ApplyMan);
            }
            if (where.Keyword.IsNotNullOrWhiteSpace())
            {
                query = query.Where(m => m.BankData.Assiginment.Contains(where.Keyword));
            }
            return query.OrderBy(m => m.Status).ThenByDescending(m => m.Created).ToPagedList(where.Page, 10);
        }
    }
}
